<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/** 
 *  Cpanel Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage News  
 * @author Manh Nguyen - 30/07/2012 
 * @link http://sunsoft.vn
 */
class Cpanel extends CI_Controller{
	/** 
	 * Contrust class
	 *  
	 * @access public 
	 */
	public function __construct(){
		parent::__construct();
		require_once(APPPATH . 'modules/backend/autoload.php');
                $this->load->library('login_manager',array('autologin' => FALSE));
	}	
	
	public function index(){
		if(!empty($_POST)){
                        $user = $this->login_manager->get_user();
                        if($user !== FALSE)
                        {
                            redirect('admin/home');
                        }
                            
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			
			if(!empty($username) && !empty($password))
			{
                            //$user = new Login_Manager();
                            $isLog = $this->login_manager->process_login($username, $password);                            
                            if($isLog==TRUE){
                                
								$this->session->set_userdata('logged_user', $user->logged_in_user);
                                $users = $this->login_manager->get_user();
                                
                                $aParamAdminLog=array(
                                                    'username'   => $users->name,
                                                    'customerID' => $users->customerID,
                                                    'email'      => $users->email,
                                                    'lastLogin'  => date("Y-m-d H:i:s"),
                                                    'logAction'  => 'Login thành công',
                                                    'ip'         => $_SERVER['REMOTE_ADDR'],
                                                    'uID'        => $user[0]->uID
                                                    );
                                $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
                                redirect('admin/home');
                            }
                            else{
                                $this->session->set_flashdata('message', $this->lang->line('login_error'));
                                
                                $aParamAdminLog=array(
                                                    'username'   => $username,
                                                    'customerID' => $username,
                                                    'email'      => $username,
                                                    'lastLogin'  => date("Y-m-d H:i:s"),
                                                    'logAction'  => 'Login thất bại',
                                                    'ip'         => $_SERVER['REMOTE_ADDR']
                                                    );
                                $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
                            }
			}else{
				$this->session->set_flashdata('message', $this->lang->line('input_empty'));
			}
		}
		redirect('admin/cpanel/login');
	}
	
        public function logout()
        {
            $this->login_manager->logout();
            redirect('admin/cpanel');
        }
        
	public function login() {
		$user = $this->login_manager->get_user();
		if($user !== FALSE)
		{
			$this->login_manager->logout();
		}
		$this->smarty->assign('login_error', $this->session->flashdata('message'));
		$this->smarty->display('backend/login');	
	}
}
/* End of file auth/cpanel.php */
/* Location: ./system/application/controllers/auth/cpanel.php */